﻿#include "setup6wind.h"
#include "ui_setup6wind.h"
#include <mainwindow.h>
extern intListener doorStatus;

setup6wind::setup6wind(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::setup6wind)
{
    logClient=loghelper::getInstance();
    setWindowIcon(QIcon(":/pic/picture/kafavicon.ico"));
    ui->setupUi(this);
    connect(&doorStatus, &intListener::valueChanged,this,&setup6wind::doorStatusChanged,Qt::QueuedConnection);
    ui->pushButton_9->setStyleSheet("background-color: rgba(0, 0, 0, 0.3);");
    ui->pushButton_9->setEnabled(false);
    ui->progressBar->setRange(0,100); //设置进度条最小值和最大值(取值范围)
    ui->progressBar->setMinimum(0); //设置进度条最小值

    ui->progressBar->reset();
}
void setup6wind::doorStatusChanged(){
    if(doorStatus.get()==0)
    {
        ui->pushButton_9->setStyleSheet("background-color: rgba(0, 0, 0, 0.3);");
    }
    else
    {
        ui->pushButton_9->setStyleSheet("background-color: rgba(0, 0, 0, 0.3);");
    }
}
setup6wind::~setup6wind()
{
    delete ui;
}

void setup6wind::showEvent(QShowEvent *event)
{
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql="Select ExperimentName from ExperimentInfo";
    query.exec(sql);
    while (query.next()) {
        syname=query.value("ExperimentName").toString();
    }
    ui->comboBox->clear();
    ui->comboBox->addItem(syname);
    tcpClient = TcpClient::getInstance();
    if(doorStatus.get()==0)
    {
        ui->pushButton_9->setStyleSheet("background-color: rgba(0, 0, 0, 0.3);");
    }
    else
    {
        ui->pushButton_9->setStyleSheet("background-color: rgba(0, 0, 0, 0.3);");
    }
    event->accept();
}

void setup6wind::on_returnBtn_clicked()
{

    this->close();
    emit changeTotalWindow();

}

void setup6wind::on_SaveBtn_2_clicked()
{
    tcts1 = new tcts();
    tcts1->infochange("提示","info","是否取消恢复?",true,true);
    tcts1->show();
    connect(tcts1->ui->pushButton_2, &QPushButton::clicked,[this](){
        ui->progressBar->setValue(0);
        syhftime=1;
        disconnect(timSend,0,0,0);
    });
}

void setup6wind::on_pushButton_9_clicked()
{
    if(doorStatus.get()==0)
    {
        doorStatus.set(1);
        ui->pushButton_9->setStyleSheet("background-color: rgba(0, 0, 0, 0.3);");
    }
    else
    {
        doorStatus.set(0);
        ui->pushButton_9->setStyleSheet("background-color: rgba(0, 0, 0, 0.3);");
    }
}
// 实验恢复7.30 cnr
void setup6wind::on_SaveBtn_clicked()
{
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);

    QString sql="delete from FluorescenceData ;";
    query.exec(sql);

    sql="Select CycleNum from AmplStep where ProgramId=(Select AmplGuid from ExperimentInfo) and "
        "CASE WHEN "
        "(Select count(*) from AmplStep where ProgramId=(Select AmplGuid from ExperimentInfo) and IsCollect='是')>0 "
        "then "
        "IsCollect='是' "
        "else "
        "StepNum=(Select max(StepNum) from AmplStep where programId=(Select AmplGuid from ExperimentInfo)) "
        "end";
    query.exec(sql);
    while (query.next()) {
        xhval=query.value("CycleNum").toInt();
    }
    qDebug()<<xhval;
    ui->progressBar->setMaximum(xhval); //设置进度条最大值
    timSend->setInterval(2000);
    connect(timSend,&QTimer::timeout,this,[=]()
    {
        if(syhftime<=xhval){
            QString type = "0007";
            QString type2 = QString("%1").arg(syhftime, 4, 16, QLatin1Char('0'));
            QString end2 = "0000";
            int len = 8;
            QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
            QString data = "6200003D"+len1+type+type2+end2+"55AA";
            QString finalData;
            for (int i = 0; i < data.length(); ++i) {
                finalData.append(data.at(i));
                // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
                if ((i+1)%2==0){
                    finalData.append(' ');
                }
            }
//            qDebug()<<finalData;
            QByteArray finalInfo=common->GetSendData(finalData);
//            qDebug()<<finalInfo;
            tcpClient->ClientSendingData(finalInfo);
            logClient->WriteLog("CommLog","发送:\n"+finalInfo);
            ui->progressBar->setValue(syhftime);  //设置当前的运行值
//            qDebug()<<xhval<<syhftime;
            syhftime++;
        }else if(syhftime>xhval){
            tcts1 = new tcts();
            tcts1->infochange("提示","treat","恢复成功",false,true);
            ui->progressBar->reset();
            syhftime=1;
            ui->progressBar->setValue(0);
            disconnect(timSend,0,0,0);
            dataAnalyJ=new DataAnalysisByJThread();
            dataAnalyJ->run(true);
            MainWindow::reAnalyse=false;
            this->hide();
            emit changeAnalyWindow();
        }
    });
    timSend->start();
}
